/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2022. All rights reserved.
 *
 */

import path from 'path';

import { BaseModifier } from './base-modifier';

/**
 * syscap.json配置文件相关处理
 */
export class SyscapJsonModifier extends BaseModifier {
  protected jsonObject!: SyscapObj;

  constructor(cwd: string, moduleName: string, target = 'main') {
    super(path.resolve(cwd, moduleName, 'src', target, 'syscap.json'));
  }

  public getGeneralDevice(): GeneralDeviceType[] {
    return this.jsonObject.devices.general;
  }

  public getSyscapList(): string[] {
    const syscapList: string[] = [];
    this.jsonObject.devices.custom.forEach((customDeviceList: any) => {
      Object.keys(customDeviceList).forEach((customDevice: string) => {
        customDeviceList[customDevice].forEach((syscap: string) => {
          syscapList.push(syscap);
        });
      });
    });
    return syscapList;
  }

  public setGeneralDevice(generalDevice: GeneralDeviceType[]) {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    this.jsonObject.devices.general = generalDevice === [GeneralDeviceType.empty] ? [] : generalDevice;
    return this;
  }

  public getAddedSyscaps(): string[] {
    return this.jsonObject.production.addedSysCaps;
  }

  public getRemovedSyscaps(): string[] {
    return this.jsonObject.production.removedSysCaps;
  }

  public addSyscap(syscap: string) {
    this.jsonObject.production.addedSysCaps.push(syscap);
    return this;
  }

  public removeSyscap(syscap: string) {
    this.jsonObject.production.removedSysCaps.push(syscap);
    return this;
  }
}

export interface SyscapObj {
  devices: DevicesObj;
  production: ProductionObj;
}

export interface DevicesObj {
  general: GeneralDeviceType[];
  custom: CustomObj[];
}

export enum GeneralDeviceType {
  empty,
  car = 'car',
  default = 'default',
  liteWearable = 'liteWearable',
  router = 'router',
  smartVision = 'smartVision',
  tablet = 'tablet',
  tv = 'tv',
  wearable = 'wearable'
}

// custom
export interface CustomObj {
  [device: string]: string[];
}

// production
export interface ProductionObj {
  addedSysCaps: string[];
  removedSysCaps: string[];
}

